﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XMK.Tadong.Model;
using XMK.Tadong.Biz;
using System.Data;
using HU8.Helpers.Files;
using System.Configuration;
using HU8.Web.FileExplorer.FileExplorer.Config;
using HU8.Web.FileExplorer;

namespace XMK.Tadong.Web.WebFileExplorer.DataSubmit
{
    public partial class AgainDataSubmit : FileExplorerPage
    {
        #region 变量

        //获取文档存储路径
        string dataPath = string.Empty;


        #endregion 变量

        #region 事件处理程序

        //提交点击
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btn_config.DataBind();
                InitData();
            }
        }

        //提交按钮点击事件
        protected void btnSunmit_Click(object sender, EventArgs e)
        {
            if (this.Request.QueryString["FileSpace_Type"].Equals("Team"))
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
                {
                    TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                    TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                    model.FILE_ID = Convert.ToInt32(this.Request.QueryString["File_ID"]);
                    model.TEAM_ID = Convert.ToInt32(hidIDS.Value);
                    model.FILE_NAME = txtWDMC.Text.Trim();
                    model.FILE_AUTHOR = txtZZ.Text.Trim();
                    model.KEYW = txtGJC.Text.Trim();
                    model.FILE_CRT_TIME = Convert.ToDateTime(paydate.Value.Trim());
                    if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                    if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                    model.USER_ID = Session["user_id"].ToString();
                    model.STATUS = 0;
                    model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    if (bll.AgainDataSubmit(model, "1"))
                    {
                        lblError.Text = lang.Get("SPACE_submit_success");
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>parent.filemanager.load_MySubmitData('" + Session["user_id"].ToString() + "');</script>");
                    }
                }
                else
                {

                    string teams = hidIDS.Value.Trim();
                    teams = teams.TrimEnd(';');
                    string[] team = teams.Split(';');
                    for (int i = 0; i < team.Length; i++)
                    {

                        TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                        TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                        model.TEAM_ID = Convert.ToInt32(team[i].Trim());
                        model.FILE_NAME = txtWDMC.Text.Trim();
                        model.FILE_AUTHOR = txtZZ.Text.Trim();
                        model.KEYW = txtGJC.Text.Trim();
                        model.FILE_CRT_TIME = Convert.ToDateTime(paydate.Value.Trim());
                        if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                        if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                        model.SAVE_PATH = "Group/" + model.TEAM_ID + "/待审核";
                        model.USER_ID = Session["user_id"].ToString();
                        model.STATUS = 0;
                        model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));


                        if (!bll.Add(model).Equals("0"))
                        {
                            //将文件复制到相应的课题组待审核的文件夹下
                            string newPath = ConfigurationManager.AppSettings["DiskPath"];
                            newPath += "Group/" + model.TEAM_ID + "/待审核";
                            string path = hidPath.Value;

                            string[] swfNames = path.Split('.');

                            string swfName = swfNames[0] + ".swf";
                            string pdffName = swfNames[0] + ".pdf";

                            FileHelper.Copy(path, newPath);
                            FileHelper.Copy(swfName, newPath);
                            FileHelper.Copy(pdffName, newPath);
                            lblError.Text = lang.Get("SPACE_submit_success");
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>parent.filemanager.load_MySubmitData(" + Session["user_id"].ToString() + ");</script>");
                        }
                    }
                }
            }
            else if (this.Request.QueryString["FileSpace_Type"].Equals("XMK"))
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
                {
                    TB_XMK_FILEInfo model = new TB_XMK_FILEInfo();
                    Tb_Xmk_FileBiz bll = new Tb_Xmk_FileBiz();
                    model.FILE_ID = Convert.ToInt32(this.Request.QueryString["File_ID"]);
                    model.TEAM_ID = Convert.ToInt32(hidIDS.Value);
                    model.FILE_NAME = txtWDMC.Text.Trim();
                    model.FILE_AUTHOR = txtZZ.Text.Trim();
                    model.KEYW = txtGJC.Text.Trim();
                    model.FILE_CRT_DATE = Convert.ToDateTime(paydate.Value.Trim());
                    if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                    if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                    model.USER_ID = Session["user_id"].ToString();
                    model.STATUS = 0;
                    model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    if (bll.AgainDataSubmit(model, "1"))
                    {
                        lblError.Text = lang.Get("SPACE_submit_success");
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>parent.filemanager.load_TeamMySubmitData(" + model.TEAM_ID + ");</script>");
                    }
                }
            }
        }

        #endregion 事件处理程序

        #region 函数集

        //页面加载函数 
        public void InitData()
        {
            string File_ID = string.Empty;
            if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
            {
                File_ID = this.Request.QueryString["File_ID"].ToString();
            }
            if (this.Request.QueryString["FileSpace_Type"].Equals("Team"))
            {
                if (!string.IsNullOrEmpty(File_ID))
                {
                    TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                    TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                    DataTable dt = bll.GetModel(File_ID);
                    if (dt.Rows.Count > 0)
                    {
                        hidIDS.Value = dt.Rows[0]["TEAM_ID"].ToString(); ;
                        lblKTZ.Text = dt.Rows[0]["team_name"].ToString();
                        txtWDMC.Text = dt.Rows[0]["FILE_NAME"].ToString();
                        txtZZ.Text = dt.Rows[0]["FILE_AUTHOR"].ToString();
                        txtGJC.Text = dt.Rows[0]["KEYW"].ToString();
                        paydate.Value = Convert.ToDateTime(dt.Rows[0]["FILE_CRT_TIME"].ToString()).ToString("yyyy-MM-dd");
                        targetValue.Value = dt.Rows[0]["DEST_OBJ"].ToString();
                        txtMB.Text = dt.Rows[0]["DEST_OBJ"].ToString();
                        bizValue.Value = dt.Rows[0]["BIZ_OBJ"].ToString();
                        txtYWDX.Text = dt.Rows[0]["BIZ_OBJ"].ToString();
                    }

                }
            }
            else if (this.Request.QueryString["FileSpace_Type"].Equals("XMK"))
            {
                div.Visible = false;
                if (!string.IsNullOrEmpty(File_ID))
                {
                    Tb_Xmk_FileBiz bll = new Tb_Xmk_FileBiz();
                    DataTable dt = bll.GetModel(File_ID);
                    if (dt.Rows.Count > 0)
                    {
                        hidIDS.Value = dt.Rows[0]["TEAM_ID"].ToString(); ;
                        lblKTZ.Text = dt.Rows[0]["team_name"].ToString();
                        txtWDMC.Text = dt.Rows[0]["FILE_NAME"].ToString();
                        txtZZ.Text = dt.Rows[0]["FILE_AUTHOR"].ToString();
                        txtGJC.Text = dt.Rows[0]["KEYW"].ToString();
                        paydate.Value = Convert.ToDateTime(dt.Rows[0]["FILE_CRT_DATE"].ToString()).ToString("yyyy-MM-dd");
                        targetValue.Value = dt.Rows[0]["DEST_OBJ"].ToString();
                        txtMB.Text = dt.Rows[0]["DEST_OBJ"].ToString();
                        bizValue.Value = dt.Rows[0]["BIZ_OBJ"].ToString();
                        txtYWDX.Text = dt.Rows[0]["BIZ_OBJ"].ToString();
                    }

                }
            }
        }
      
        #endregion 函数集
    }
}